package com.erp.order.purchasing.controller;

import com.erp.hr.base.controller.BaseController;
import com.erp.masterdata.vendor.dao.model.MdVendor;
import com.erp.masterdata.vendor.service.MdVendorService;
import com.erp.order.purchasing.dao.InquiryListDao;
import com.erp.order.purchasing.dao.InquiryListMaterialDao;
import com.erp.order.purchasing.model.*;
import com.erp.order.purchasing.service.InquiryListMaterialService;
import com.erp.order.purchasing.service.InquiryListService;
import com.erp.order.purchasing.service.QuotedPriceService;
import org.redragon.framework.hibernate.model.Pages;
import org.redragon.framework.util.ShiroUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/*
 * 供应商询价单
 */


@Controller
@RequestMapping("/web/mdVendorInquiry")
public class MdVendorInquiryListController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(MdVendorInquiryListController.class);

    @Autowired
    private InquiryListService inquiryListService;

    @Autowired
    private QuotedPriceService quotedPriceService;

    @Autowired
    private InquiryListMaterialService inquiryListMaterialService;

    @Autowired
    private MdVendorService mdVendorService;

    @Autowired
    private InquiryListDao inquiryListDao;

    @Autowired
    private InquiryListMaterialDao materialDao;

    //获取供应商询价单
    @RequestMapping("getInquiryList")
    public String getInquiryList(Pages pages, InquiryListR inquiryListR, Model model) {
        //当前登录人的登录名
        String username= ShiroUtil.getPrincipal();
        //分页查询数据
        if(pages.getPage()==0) {
            pages.setPage(1);
        }

        //分页查询数据
        List<InquiryListR>  inquiryList = this.inquiryListService.getInquiryLists(pages,inquiryListR, username);

        //页面属性设置
        model.addAttribute("procurementTypeMap", inquiryListService.getDictByKey("procurement_type"));
        model.addAttribute("inquiryList", inquiryList);
        model.addAttribute("inquiryTypeMap", inquiryListService.getDictByKey("procurement_type"));
        return "basic.jsp?content=mdVendor/myInquiryList";
    }

    //获取供应商订单
    @RequestMapping("getMyOrderList")
    public String getMyOrderList(Pages pages, InquiryListRS inquiryListR, Model model) {
        //当前登录人的登录名
        String username= ShiroUtil.getPrincipal();

        //分页查询数据
        if(pages.getPage()==0) {
            pages.setPage(1);
        }
        //分页查询数据
        List<InquiryListRS>  inquiryList = this.inquiryListService.getQuotedLists(pages,inquiryListR, username);
        //页面属性设置
        model.addAttribute("quotedList", inquiryList);
        model.addAttribute("procurementTypeMap", inquiryListService.getDictByKey("procurement_type"));
        model.addAttribute("inquiryTypeMap", inquiryListService.getDictByKey("procurement_type"));
        return "basic.jsp?content=mdVendor/myOrders";
    }

    @RequestMapping("getInquiryInfo")
    public String getInquiryInfo(InquiryList inquiry, Model model){
        int id= Math.toIntExact(inquiry.getId());
        InquiryList inquiryList=this.inquiryListService.getDataObject(id);
        List<InquiryListMaterial>  materials=this.inquiryListMaterialService.getDataObjects(id);
        //页面属性设置
        model.addAttribute("inquiryList", inquiryList);
        model.addAttribute("materials", materials);
        //加载数据字典
        model.addAttribute("procurementTypeMap", inquiryListService.getDictByKey("procurement_type"));
        model.addAttribute("inquiryTypeMap", inquiryListService.getDictByKey("inquiry_type"));
        model.addAttribute("quoteRequestMap", inquiryListService.getDictByKey("quote_request"));
        model.addAttribute("tradingMannerMap", inquiryListService.getDictByKey("trading_manner"));
        model.addAttribute("designatedSettlementDateMap", inquiryListService.getDictByKey("designated_settlement_date"));
        model.addAttribute("settlementDateMap", inquiryListService.getDictByKey("settlement_date"));
        model.addAttribute("paymentStageMap", inquiryListService.getDictByKey("payment_stage"));
        model.addAttribute("paymentMap", inquiryListService.getDictByKey("payment"));
        model.addAttribute("invoiceRequirementsMap", inquiryListService.getDictByKey("invoice_requirements"));
        model.addAttribute("certificationRequirementsMap", inquiryListService.getDictByKey("certification_requirements"));
        model.addAttribute("bookBuildingMap", inquiryListService.getDictByKey("book_building"));
        model.addAttribute("procurementPortalMap", inquiryListService.getDictByKey("procurement_portal"));
        model.addAttribute("priceComparisonWayMap", inquiryListService.getDictByKey("price_comparison_way"));
        model.addAttribute("contactWayMap", inquiryListService.getDictByKey("contact_way"));
        model.addAttribute("unitMap", inquiryListService.getDictByKey("material_unit"));

        return "basic.jsp?content=mdVendor/myInquiryInfo";
    }

    //去报价
    @RequestMapping("quotedPrice")
    public String quotedPrice(InquiryList inquiry, Model model){
        String username= ShiroUtil.getPrincipal();
        int id= Math.toIntExact(inquiry.getId());
        List<InquiryListMaterial>  materials=this.inquiryListMaterialService.getDataObjects(id);
        MdVendor mdVendor=this.mdVendorService.getDataByCredictCode(username);
        //页面属性设置
        model.addAttribute("inquiryList", inquiryListService.addCompleteData(
                inquiryListService.getDataObject(
                        inquiry.getId().toString())));
        model.addAttribute("materials", materials);
        model.addAttribute("mdVendor", mdVendor);
        model.addAttribute("procurementTypeMap", inquiryListService.getDictByKey("procurement_type"));
        model.addAttribute("inquiryTypeMap", inquiryListService.getDictByKey("inquiry_type"));
        model.addAttribute("quoteRequestMap", inquiryListService.getDictByKey("quote_request"));
        model.addAttribute("tradingMannerMap", inquiryListService.getDictByKey("trading_manner"));
        model.addAttribute("designatedSettlementDateMap", inquiryListService.getDictByKey("designated_settlement_date"));
        model.addAttribute("settlementDateMap", inquiryListService.getDictByKey("settlement_date"));
        model.addAttribute("paymentStageMap", inquiryListService.getDictByKey("payment_stage"));
        model.addAttribute("paymentMap", inquiryListService.getDictByKey("payment"));
        model.addAttribute("invoiceRequirementsMap", inquiryListService.getDictByKey("invoice_requirements"));
        model.addAttribute("certificationRequirementsMap", inquiryListService.getDictByKey("certification_requirements"));
        model.addAttribute("bookBuildingMap", inquiryListService.getDictByKey("book_building"));
        model.addAttribute("procurementPortalMap", inquiryListService.getDictByKey("procurement_portal"));
        model.addAttribute("priceComparisonWayMap", inquiryListService.getDictByKey("price_comparison_way"));
        model.addAttribute("contactWayMap", inquiryListService.getDictByKey("contact_way"));
        model.addAttribute("procurementMethodMap", inquiryListService.getDictByKey("procurement_method"));
        model.addAttribute("unitMap", inquiryListService.getDictByKey("material_unit"));
        return "basic.jsp?content=mdVendor/quotedPrice";
    }
    //查看报价详情
    @RequestMapping("getPriceInfo")
    public String getPriceInfo(QuotedPrice quotedPrice, Model model){
        String username= ShiroUtil.getPrincipal();
        //String username= "8978iyuh";
        //String username= "123123wA998iidda";
        //String username= "sffvtrbgnj";
        Long id= quotedPrice.getId();
        //根据询价单id获取报价详情
        List<QuotedPrice> quotedPriceInfo=this.quotedPriceService.getData(String.valueOf(quotedPrice.getInquiryListId()),username);
        List<QuotePriceInfo>  quotePriceInfoList = this.inquiryListService.getPriceInfoById(id);

        //页面属性设置
        model.addAttribute("inquiryList", inquiryListService.addCompleteData(
                inquiryListService.getDataObject(
                        quotedPrice.getInquiryListId().toString())));

        model.addAttribute("procurementTypeMap", inquiryListService.getDictByKey("procurement_type"));
        model.addAttribute("inquiryTypeMap", inquiryListService.getDictByKey("inquiry_type"));
        model.addAttribute("quoteRequestMap", inquiryListService.getDictByKey("quote_request"));
        model.addAttribute("tradingMannerMap", inquiryListService.getDictByKey("trading_manner"));
        model.addAttribute("designatedSettlementDateMap", inquiryListService.getDictByKey("designated_settlement_date"));
        model.addAttribute("settlementDateMap", inquiryListService.getDictByKey("settlement_date"));
        model.addAttribute("paymentStageMap", inquiryListService.getDictByKey("payment_stage"));
        model.addAttribute("paymentMap", inquiryListService.getDictByKey("payment"));
        model.addAttribute("invoiceRequirementsMap", inquiryListService.getDictByKey("invoice_requirements"));
        model.addAttribute("certificationRequirementsMap", inquiryListService.getDictByKey("certification_requirements"));
        model.addAttribute("bookBuildingMap", inquiryListService.getDictByKey("book_building"));
        model.addAttribute("procurementPortalMap", inquiryListService.getDictByKey("procurement_portal"));
        model.addAttribute("priceComparisonWayMap", inquiryListService.getDictByKey("price_comparison_way"));
        model.addAttribute("contactWayMap", inquiryListService.getDictByKey("contact_way"));
        model.addAttribute("procurementMethodMap", inquiryListService.getDictByKey("procurement_method"));
        model.addAttribute("unitMap", inquiryListService.getDictByKey("material_unit"));
        model.addAttribute("quotedPriceInfo", quotedPriceInfo.get(0));
        model.addAttribute("quotePriceInfoList", quotePriceInfoList);
        //页面属性设置
        System.out.print(12);
        return "basic.jsp?content=mdVendor/quotedPriceInfo";
    }
}
